package com.cskaoyan.service.impl;

import com.cskaoyan.bean.CskaoyanmallAddress;
import com.cskaoyan.bean.CskaoyanmallAddressExample;
import com.cskaoyan.bean.vo.WxAddressVo;
import com.cskaoyan.mapper.CskaoyanmallAddressMapper;
import com.cskaoyan.service.AddressService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AddressServiceImpl implements AddressService {
        @Autowired
        CskaoyanmallAddressMapper addressMapper ;

        @Override
        public Map<String, Object> querySelective(Integer userId, String name, Integer page, Integer limit, String sort, String order) {
            PageHelper.startPage(page, limit);
            CskaoyanmallAddressExample addressExample = new CskaoyanmallAddressExample();
            CskaoyanmallAddressExample.Criteria criteria = addressExample.createCriteria();
            if (!ObjectUtils.isEmpty(userId)) {
                criteria.andUserIdEqualTo(userId);
            }
            if (!ObjectUtils.isEmpty(name)) {
                criteria.andNameLike("%" + name + "%");
            }
            if (!ObjectUtils.isEmpty(sort) && !ObjectUtils.isEmpty(order)) {
                addressExample.setOrderByClause(sort + " " + order);
            }


            List<CskaoyanmallAddress> addresses = addressMapper.selectByExample(addressExample);
            PageInfo<CskaoyanmallAddress> addressPageInfo = new PageInfo<>(addresses);
            long total = addressPageInfo.getTotal();
            Map<String, Object> res = new HashMap<>();
            res.put("items", addresses);
            res.put("total", total);

            return res;
    }

    @Override
    public List<WxAddressVo> queryAll() {
        List<WxAddressVo> addresses = addressMapper.selectAll();
        return addresses;
    }

    @Override
    public CskaoyanmallAddress queryAddressById(Integer id) {
        CskaoyanmallAddress address = addressMapper.selectByPrimaryKey(id);
        return address;
    }

    @Override
    public Integer deleteAddressById(CskaoyanmallAddress cskaoyanmallAddress) {
        Integer i = addressMapper.deleteAddressById(cskaoyanmallAddress.getId());
        return i;
    }

    @Override
    public Integer insertAddress(CskaoyanmallAddress cskaoyanmallAddress) {
        int i = addressMapper.insertSelective(cskaoyanmallAddress);
        return i;
    }

    @Override
    public Integer updateAddress(CskaoyanmallAddress cskaoyanmallAddress) {
        int i = addressMapper.updateByPrimaryKeySelective(cskaoyanmallAddress);
        return i;
    }
}
